package com.tbdgroup.first.babytools;

import java.util.ArrayList;
import java.util.List;

import com.tbdgroup.first.babytools.TSEntryContract.TSEntry;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TSEntryDbHelper extends SQLiteOpenHelper  {
	private static final String TEXT_TYPE = " TEXT";
//	private static final String TS_TYPE = " TIMESTAMP";
	private static final String COMMA_SEP = ",";
	private static final String SQL_CREATE_ENTRIES =
	    "CREATE TABLE " + TSEntry.TABLE_NAME + " (" +
	    TSEntry._ID + " INTEGER PRIMARY KEY," +
//	    TSEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
	    TSEntry.COLUMN_NAME_BABY_ID + TEXT_TYPE + COMMA_SEP +
	    TSEntry.COLUMN_NAME_START_TS + TEXT_TYPE + COMMA_SEP +
	    TSEntry.COLUMN_NAME_END_TS + TEXT_TYPE + COMMA_SEP +
	    TSEntry.COLUMN_NAME_TPYE + TEXT_TYPE + COMMA_SEP +
	    TSEntry.COLUMN_NAME_VALID_FLAG + TEXT_TYPE +
	    " )";

	private static final String SQL_DELETE_ENTRIES =
	    "DROP TABLE IF EXISTS " + TSEntry.TABLE_NAME;
	
	private static final String SQL_CLEAN_ENTRIES =
		    "DELETE FROM " + TSEntry.TABLE_NAME;	
	
	 // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "TSEntry.db";

    public TSEntryDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        
    }
    public void onCreate(SQLiteDatabase db) {
  //  	Log.i("tag", "db created");
        db.execSQL(SQL_CREATE_ENTRIES);
    }   
    
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
    
    public void cleanDB(){
    	this.getWritableDatabase().execSQL(SQL_CLEAN_ENTRIES);
    }
    public long putStartTS(String type, String TS, String babyID){
    	// Gets the data repository in write mode
    	SQLiteDatabase db = this.getWritableDatabase();

    	// Create a new map of values, where column names are the keys
    	ContentValues values = new ContentValues();
    	values.put(TSEntry.COLUMN_NAME_BABY_ID, babyID);
    	values.put(TSEntry.COLUMN_NAME_START_TS, TS);
    	values.put(TSEntry.COLUMN_NAME_TPYE, type);

    	// Insert the new row, returning the primary key value of the new row
    	return db.insert(TSEntry.TABLE_NAME, null, values);    	
    }
    
    
    public void upateEndTS(String TS, long entryID){
    	// Gets the data repository in write mode
    	SQLiteDatabase db = this.getWritableDatabase();

    	// Create a new map of values, where column names are the keys
    	ContentValues values = new ContentValues();
    	values.put(TSEntry.COLUMN_NAME_END_TS, TS);

    	// Which row to update, based on the ID
    	String selection = TSEntry._ID + " LIKE ?";
    	String[] selectionArgs = { String.valueOf(entryID) };
    	db.update(TSEntry.TABLE_NAME, values, selection,  selectionArgs);
    }
    
    public void updateValid(String entryID, String validFlag){
    	SQLiteDatabase db = this.getWritableDatabase();

    	ContentValues values = new ContentValues();
    	values.put(TSEntry.COLUMN_NAME_VALID_FLAG, validFlag);

    	String selection = TSEntry._ID + " LIKE ?";
    	String[] selectionArgs = { String.valueOf(entryID) };

    	db.update(TSEntry.TABLE_NAME, values, selection,  selectionArgs);
    }
    
    public List<DisplayEntry> getAllEntry(){
    	SQLiteDatabase db = this.getReadableDatabase();
    	List<DisplayEntry> entries = new ArrayList<DisplayEntry>();
    	
    	// Define a projection that specifies which columns from the database
    	// you will actually use after this query.
    	String[] projection = {
    		TSEntry._ID,
    		TSEntry.COLUMN_NAME_BABY_ID,
    		TSEntry.COLUMN_NAME_TPYE,
    	    TSEntry.COLUMN_NAME_START_TS,
    	    TSEntry.COLUMN_NAME_END_TS,
    	    TSEntry.COLUMN_NAME_VALID_FLAG
    	    };

    	// How you want the results sorted in the resulting Cursor
    	String sortOrder =
    			TSEntry._ID + " DESC";

    	Cursor cursor = db.query(
    		TSEntry.TABLE_NAME,  // The table to query
    	    projection,                               // The columns to return
    	    null,                             		   // The columns for the WHERE clause
    	    null,                          			  // The values for the WHERE clause
    	    null,                                     // don't group the rows
    	    null,                                     // don't filter by row groups
    	    sortOrder                                 // The sort order
    	    );
    	
    	if(!cursor.moveToFirst()){
    		return entries;
    	}
    	
    	while(!cursor.isAfterLast()){
    		long id = cursor.getLong(
    	    	    cursor.getColumnIndexOrThrow(TSEntry._ID)
    		    	);
    		String babyid = cursor.getString(
    	    	    cursor.getColumnIndexOrThrow(TSEntry.COLUMN_NAME_BABY_ID)
    		    	);
    		String type = cursor.getString(
    	    	    cursor.getColumnIndexOrThrow(TSEntry.COLUMN_NAME_TPYE)
    		    	);
    		String sts = cursor.getString(
    	    	    cursor.getColumnIndexOrThrow(TSEntry.COLUMN_NAME_START_TS)
    		    	);
    		String ets = cursor.getString(
    	    	    cursor.getColumnIndexOrThrow(TSEntry.COLUMN_NAME_END_TS)
    		    	);
    		String valid = cursor.getString(
    	    	    cursor.getColumnIndexOrThrow(TSEntry.COLUMN_NAME_VALID_FLAG)
    		    	);
    		DisplayEntry de = new DisplayEntry(id,babyid,type,sts,ets,valid);
    		entries.add(de);
    		cursor.moveToNext();
    	}
    	cursor.close();
    	
    	 return entries;
    }   
}
